<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="table.where" label-width="77px" class="ele-form-search"
               @keyup.enter.native="$refs.table.reload()" @submit.native.prevent>
        <el-row :gutter="15">
              <!-- 任务状态下拉单选 -->
          <el-col :md="4" :sm="12">
            <el-form-item label="任务状态:">
              <el-select v-model="table.where.status" placeholder="请选择任务状态" clearable class="ele-fluid">
                <el-option label="全部" :value="''"/>
                <el-option label="进行中" :value="0"/>
                <el-option label="失败" :value="1"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="12">
            <div class="ele-form-actions">
              <el-button type="primary" @click="$refs.table.reload()" icon="el-icon-search" class="ele-btn-icon">查询
              </el-button>
              <el-button @click="(table.where={})&&$refs.table.reload()">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 操作按钮 -->
      <div class="ele-table-tool ele-table-tool-default">
        <el-button @click="showEdit=true" type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small" v-if="permission.includes('sys:taskmy:add')">添加
        </el-button>
        <el-button @click="remove()" type="danger" icon="el-icon-delete" class="ele-btn-icon" size="small" v-if="permission.includes('sys:taskmy:dall')">批量删除
        </el-button>
      </div>
      <!-- 数据表格 -->
      <ele-data-table ref="table" :config="table" :choose.sync="choose" height="calc(100vh - 315px)" highlight-current-row>
        <template slot-scope="{index}">
          <el-table-column type="selection" width="45" align="center" fixed="left"/>
          <el-table-column type="index" :index="index" label="编号" width="60" align="center" fixed="left" show-overflow-tooltip/>
          <el-table-column prop="adminId" label="用户ID" align="center" show-overflow-tooltip min-width="200"/>
          <el-table-column prop="userId" label="用户ID" align="center" show-overflow-tooltip min-width="200"/>
          <el-table-column prop="taskId" label="任务ID" align="center" show-overflow-tooltip min-width="200"/>
            <el-table-column prop="status" label="任务状态" align="center" min-width="100">
              <template slot-scope="{row}">
                <ele-dot :type="['success', 'danger', 'primary', 'warning', 'info'][row.status-1]" :ripple="row.status===0"
                         :text="['进行中','已完成','失败'][row.status-1]"/>
              </template>
            </el-table-column>
          <el-table-column prop="csc" label="完成进度%" align="center" show-overflow-tooltip min-width="200"/>
          <el-table-column prop="createTime" label="创建时间" align="center" show-overflow-tooltip min-width="200"/>
          <el-table-column prop="updateTime" label="最后操作时间" align="center" show-overflow-tooltip min-width="200"/>
          <el-table-column label="其他周期任务保存任务发布时间 " align="center" show-overflow-tooltip min-width="160">
            <template slot-scope="{row}">{{ row.cycledate | toDateString }}</template>
          </el-table-column>
          <el-table-column label="操作" width="130px" align="center" :resizable="false"  fixed="right">
            <template slot-scope="{row}">
              <el-link @click="edit(row)" icon="el-icon-edit" type="primary" :underline="false" v-if="permission.includes('sys:taskmy:edit')">修改</el-link>
              <el-popconfirm title="确定要删除此我的网评任务表吗？" @confirm="remove(row)" class="ele-action">
                <el-link slot="reference" icon="el-icon-delete" type="danger" :underline="false" v-if="permission.includes('sys:taskmy:delete')">删除</el-link>
              </el-popconfirm>
            </template>
          </el-table-column>
        </template>
      </ele-data-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <el-dialog :title="editForm.id?'修改我的网评任务表':'修改我的网评任务表'" :visible.sync="showEdit" width="400px"
               @closed="editForm={}" :destroy-on-close="true" :lock-scroll="false">
      <el-form :model="editForm" ref="editForm" :rules="editRules" label-width="82px">
        <el-form-item label="用户ID:" prop="admin_id">
          <el-input-number v-model="editForm.admin_id" controls-position="right" :min="0"
                           placeholder="请输入用户ID" class="ele-fluid ele-text-left"/>
        </el-form-item>
        <el-form-item label="用户ID:" prop="user_id">
          <el-input-number v-model="editForm.user_id" controls-position="right" :min="0"
                           placeholder="请输入用户ID" class="ele-fluid ele-text-left"/>
        </el-form-item>
        <el-form-item label="任务ID:" prop="task_id">
          <el-input-number v-model="editForm.task_id" controls-position="right" :min="0"
                           placeholder="请输入任务ID" class="ele-fluid ele-text-left"/>
        </el-form-item>
          <el-form-item label="任务状态:" prop="status">
            <el-select v-model="editForm.status" placeholder="请选择任务状态" class="ele-block" clearable>
                  <el-option label="进行中" :value="0"/>
                  <el-option label="失败" :value="1"/>
            </el-select>
          </el-form-item>
        <el-form-item label="其他周期任务保存任务发布时间 :" prop="cycledate">
          <el-date-picker
                  v-model="editForm.cycledate"
                  type="datetime"
                  placeholder="选择其他周期任务保存任务发布时间 "
                  size="small"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="showEdit=false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
  import { mapGetters } from "vuex";
  export default {
    name: "SysTaskMy",
    data() {
      return {
        table: {url: '/taskmy/index', where: {}},  // 表格配置
        choose: [],  // 表格选中数据
        showEdit: false,  // 是否显示表单弹窗
        editForm: {},  // 表单数据
        editRules: {  // 表单验证规则

        },
      }
    },
    computed: {
      ...mapGetters(["permission"]),
    },
    mounted() {
    },
    methods: {
      /* 显示编辑 */
      edit(row) {
        this.$http.get('/taskmy/info/' + row.id).then(res => {
          if (res.data.code === 0) {
            this.editForm = res.data.data;
            this.showEdit = true;
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          this.$message.error(e.message);
        });
      },
      /* 保存编辑 */
      save() {
        this.$refs['editForm'].validate((valid) => {
          if (valid) {
            const loading = this.$loading({lock: true});
            this.$http[this.editForm.id ? 'put' : 'post'](this.editForm.id ? '/taskmy/edit' : '/taskmy/add', this.editForm).then(res => {
              loading.close();
              if (res.data.code === 0) {
                this.showEdit = false;
                this.$message({type: 'success', message: res.data.msg});
                this.$refs.table.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            }).catch(e => {
              loading.close();
              this.$message.error(e.message);
            });
          } else {
            return false;
          }
        });
      },
      /* 删除 */
      remove(row) {
        if (!row) {  // 批量删除
          if (this.choose.length === 0) return this.$message.error('请至少选择一条数据');
          let ids = this.choose.map(d => d.id);
          this.$confirm('确定要删除选中的我的网评任务表吗?', '提示', {type: 'warning'}).then(() => {
            const loading = this.$loading({lock: true});
            this.$http.delete('/taskmy/delete/' + ids).then(res => {
              loading.close();
              if (res.data.code === 0) {
                this.$message({type: 'success', message: res.data.msg});
                this.$refs.table.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            }).catch(e => {
              loading.close();
              this.$message.error(e.message);
            });
          }).catch(() => 0);
        } else {  // 单个删除
          const loading = this.$loading({lock: true});
          this.$http.delete('/taskmy/delete/' + [row.id]).then(res => {
            loading.close();
            if (res.data.code === 0) {
              this.$message({type: 'success', message: res.data.msg});
              this.$refs.table.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        }
      },
    }
  }
</script>

<style scoped>
  .ele-block >>> .el-upload, .ele-block >>> .el-upload-dragger {
    width: 100%;
  }
</style>